package com.filldream.fastboot.web.controller.common;

import com.filldream.fastboot.base.mapper.ClsNewsMapper;
import com.filldream.fastboot.common.entity.B;
import com.filldream.fastboot.common.entity.R;
import com.filldream.fastboot.common.okutil.cls.dto.ClsNewsSimpleItem;
import com.filldream.fastboot.core.manager.ClsNewsManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@AllArgsConstructor
@Api(tags = "财联社接口")
@RestController
@RequestMapping("/common/cls")
public class ClsController {

    private final ClsNewsMapper clsNewsMapper;

    private final ClsNewsManager clsNewsManager;

    @GetMapping(value = "/news")
    @ApiOperation(value = "财联社头条新闻",notes = "财联社头条新闻")
    public R<List<ClsNewsSimpleItem>> news() {
        List<ClsNewsSimpleItem> result = clsNewsMapper.getPublicNews();
        clsNewsMapper.modifyPublicNews(result.stream().map(ClsNewsSimpleItem::getDbId).collect(Collectors.toList()));
        return R.autoSuccess( result );
    }

    @Scheduled(cron = "35 * * * * ? ")
    @PostMapping("/autoSend")
    @ApiOperation(value = "自动发送财联社新闻到WebSocket")
    public R testWs(){
        return clsNewsManager.sendWs();
    }

    @GetMapping(value = "/cls/refresh")
    @ApiOperation(value = "同步财联社头条",notes = "同步财联社头条")
    public B newsRefresh() {
        return clsNewsManager.refreshClsNews();
    }

    @GetMapping(value = "/cls/autoSendOwner")
    @ApiOperation(value = "发送财联社头条机器人",notes = "发送财联社头条机器人")
    public B newsAutoSendOwner() {
        return clsNewsManager.autoSendOwner(Arrays.asList("zr","sun"),1,true);
    }


    @GetMapping(value = "/cls/autoAnchor")
    @ApiOperation(value = "同步财联社板块",notes = "发送财联社板块机器人")
    public B newsAutoAnchor() {
        clsNewsManager.refreshClsAnchor();
        return B.success();
    }

}
